/**
 * lvConfirm 确认框
 * 
 * options: 支持jquery.ui.dialog的所有属性
 * 新增如下属性：
 * 	html：设置弹出框的内容
 * 	btnOk：设置确认按钮，支持jquery的属性设置方法。默认按钮文字为“确定”，点击后关闭对话框。如
 * 		btnOk: {text: "OK", click: function(){ alert("select 'OK' btn;");}}
 * 
 * 	btnCancel：设置取消按钮，支持jquery的属性设置方法。默认按钮文字为“取消”，点击后关闭对话框
 * 		btnCancel: {text: "OK", click: function(){ alert("select 'Cancel' btn;");}}
 * 
 */
(function($) {
	$.fn.lvDialog = function(opts) {
		options = $.extend( {}, $.fn.lvDialog.defaults, opts);

		return this.each(function() {
			var $dialog = $(this);
			$(this).dialog(options);
			if(options.post == true) {
				var buttons = $(this).dialog( "option", "buttons" );
				buttons[0] = {
					text: "确定",
                    click: function() {
						$.post(options.url, $("form").serialize(), function(data){
							if(data.error == 0){
								$dialog.html(data.message);
								if(options.complate) {
									options.complate(data.params);
								}
								$dialog.dialog("option", "buttons", [{
									text: "确定",
									click: function() {
										$dialog.dialog("close");
									}
								}]);
							} else {
								$dialog.html(data.message);
								$dialog.dialog("option", "buttons", [{
									text: "取消",
									click: function() {
										$dialog.dialog("close");
									}
								}]);
							}
						});
					}
                };
				$(this).dialog("option", "buttons", buttons);
        	}
		});
	};
	
	$.fn.lvDialog.defaults = {
			modal: true,
			height: 450,
			width: 500,
			resizable: false,
			buttons: [
			    {
			        text: "确定",
			        click: function() { 
			        	
			        	$(this).dialog("close");
			        }
			    },
			    {
			        text: "取消",
			        click: function() { $(this).dialog("close"); }
			    }
			]
		};
	
	$.fn.lvConfirm = function(opts){
		options = $.extend( {}, $.fn.lvConfirm.defaults, opts);

		return this.each(function() {
			var $dialog = $(this);
			if(options.html != '') {
				$dialog.html(options.html);
			}
			if(options.btnOk != '') {
				options.buttons[0] = $.extend({}, options.buttons[0], options.btnOk);
			}
			if(options.btnCancel != '') {
				options.buttons[1] = $.extend({}, options.buttons[1], options.btnCancel);
			}
			$(this).dialog(options);
		});
	};
	$.fn.lvConfirm.defaults = {
			modal: true,
			height: 200,
			width: 500,
			resizable: false,
			buttons: [
			    {
			        text: "确定",
			        id: "dialog-ok",
			        click: function() { 
			        	$(this).dialog("close");
			        	return true;
			        }
			    },
			    {
			        text: "取消",
			        id: "dialog-cancel",
			        click: function() { $(this).dialog("close"); }
			    }
			]
		};
})(jQuery);


/**
 * JQuery AJAX 默认设置,及错误设置
 */
(function () { 
	$.ajaxSetup({
		type: "POST",
		dataType: "json"
	});
	
	var message = function (type, msg) {
		var template = {
			success: '<div class="message-success ui-corner-all"><span class="ui-icon ui-icon-circle-check"></span>#msg#</div>',
			alter: '<div class="ui-state-highlight ui-corner-all"><span class="ui-icon ui-icon-info"></span>#msg#</div>',
			error: '<div class="ui-state-error ui-corner-all"><span class="ui-icon ui-icon-alert"></span>#msg#</div>'
		};
		$(template[type].replace(/#msg#/, msg))
			.appendTo(this)
			.delay(1800)
			.fadeOut(900, function (){ $(this).remove(); });
	};
	
	var errorFunc = function(event,request, settings){
		if (request.status != 200 || settings.dataType != 'json') {
			message.call(this, 'error', '系统错误!');
		} else {
			try {
				var result = $.parseJSON(request.responseText);
				message.call(this, 'error', result.msg?result.msg:'系统错误!');
			} catch (e) {
				message.call(this, 'error', '返回结果格式错误!');
			}
		}
	};
	
	$(function (){
		$("#message").ajaxSuccess(function(event, request, settings){
			if (settings.dataType != 'json') return ;
			var result = $.parseJSON(request.responseText);
			if (!result.tip) return ;
			
			if (!result.err) {
				message.call(this, 'success', result.msg?result.msg:'操作成功');
			} else {
				errorFunc.call(this, event, request, settings);
			}
		});
		
		$("#message").ajaxError(errorFunc);
		
		$('a[target=_newTab]').live('click', function (){
			window.parent.$("#layout-main-tabs").tabs( "add", this.href, 'Temp');
			return false;
		});
	});
})();

function alertAttr(obj, like) {
	var a;
	if (like) {
		for (var i in obj) {
			try {
				if (i.search(like) != -1) {
					a+=i+':'+obj[i]+"\n";
				}
			} catch (e) {
				try {
					a+=i+':'+typeof(obj[i]);
				} catch (e) {
					a+=i+":\n";
				}
			}
		}
	}  else {
		for (var i in obj) {
			try {
				a+=i+':'+obj[i]+"\n";
			} catch (e) {
				try {
					a+=i+':'+typeof(obj[i]);
				} catch (e) {
					a+=i+":\n";
				}
			}
		}
	}
	alert(a);
}

var navTab = {getCurrentPanel: function () {
    return document;
}}